package com.lingjtx.common.thread.mq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class MessageCompressionUtils {
    public static MessagePostProcessor gzipCompressionProcessor() {
        return message -> {
            try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
                 GZIPOutputStream gzip = new GZIPOutputStream(bos)) {

                gzip.write(message.getBody());
                gzip.finish();
                return new Message(bos.toByteArray(), message.getMessageProperties());
            } catch (IOException e) {
                throw new RabbitSenderTemplate.RabbitOperationException("消息压缩失败", e);
            }
        };
    }

}
